import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:dio/dio.dart';


class List extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('列表页面'),
        ),
      body: new Center(
        child: new InfiniteListView(),
      ),
    );
  }
}


class InfiniteListView extends StatefulWidget {
  @override
  _InfiniteListViewState createState() => new _InfiniteListViewState();
}

class  _InfiniteListViewState extends State<InfiniteListView>{
  static const loadingTag = '##loading##'; // 表尾标记
//  var _words = <String>[loadingTag];
  var _words = [];

  @override
  void initState() {
    _retrieveData();
  }

  void _retrieveData() async {
//    Future.delayed(Duration(seconds: 2)).then((e) {
//      _words.insertAll(_words.length - 1,
//          //每次生成20个单词
//          generateWordPairs().take(20).map((e) => e.asPascalCase).toList()
//      );
//    Dio dio = new Dio();
//    Response<Map> response = await dio.get("http://server.58yilu.com/coin/coin");

//    print(response.data['body'].toString());
//      setState(() {
//        //重新构建列表
//        _words = response.data['body'];
//      });
//    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.separated(
        itemBuilder: (context, index){
          // 如果到了表尾
          if(_words[index] == loadingTag) {
            // 不足100条，继续获取数据
            if(_words.length -1 < 100) {
              // 获取数据
//              _retrieveData();

              // 加载时显示loading
              return Container(
                padding: const EdgeInsets.all(16),
                alignment: Alignment.center,
                child: SizedBox(
                  width: 24,
                  height: 24,
                  child: CircularProgressIndicator(strokeWidth: 2,),
                ),
              );
            } else {
              // 已经加载了100条数据，不再获取数据
              return Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(16),
                child: Text('没有更多了',style: TextStyle(color: Colors.green),),
              );
            }
          }
          print(_words[index]);
//          return  ListTile(title: Text('名称：'+_words[index]['coin']+'  阈值：'+_words[index]['zone'].toString()));
          return  new Column(
            children: <Widget>[
              ListTile(
                title: Text('名称：'+_words[index]['coin']+'  阈值：'+_words[index]['zone'].toString()),
                subtitle: new Text(_words[index]['coinName']),
                leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
              ),
            ],
          );
        },

        separatorBuilder: (context, index) => Divider(height: .0),
        itemCount: _words.length
    );
  }
}
